package com.controlstudent.android.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.controlstudent.android.app.entidades.Apoderado;
import com.controlstudent.android.app.util.URLUtil;
import com.nlcode.util.gsonxml.GsonXmlUtil;
import com.stanfy.jsonxml.GsonXml;

public class MainActivity extends Activity {

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText txtUsuario = (EditText) findViewById(R.id.editText);

        final EditText txtPassword = (EditText) findViewById(R.id.editText2);

        Button btnIniciarSesion = (Button) findViewById(R.id.button2);
        btnIniciarSesion.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                try {
                    String url = "http://192.168.1.33:8084/SCWebServices/webresources/studentcontrolwsport/acceso/?"
                            + "usuario=" + txtUsuario.getText() + "&"
                            + "clave=" + txtPassword.getText();

                    String xml = URLUtil.getContent(url);

                    GsonXml gxml = GsonXmlUtil.getGsonXml();
                    Apoderado apod = gxml.fromXml(xml, Apoderado.class);

                    if (apod != null && apod.getApod_id() != 0) {
                        Intent i = new Intent(MainActivity.this, MenuPrincipal.class);

                        Bundle objBundle = new Bundle();
                        objBundle.putSerializable("com.controlstudent.android.app.apoderado", apod);
                        i.putExtras(objBundle);
                        startActivity(i);
                    } else {
                        Toast.makeText(MainActivity.this, "Usuario incorrecto", Toast.LENGTH_LONG).show();
                    }

                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }

            }
        });

    }
}
